package com.zdyt.opc.controller;


import com.zdyt.opc.common.ResultEntity;
import com.zdyt.opc.entity.Item;
import com.zdyt.opc.entity.ItemTask;
import com.zdyt.opc.entity.SampAccessTask;
import com.zdyt.opc.service.OpcCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/opc/collect")
public class OpcCollectController {


    @Autowired
    OpcCollectService opcCollectService;


    // =====================采集任务相关==========================
    /**
     *  添加采集任务
     */
    @PostMapping("/add/sampTask")
    public ResultEntity<SampAccessTask> addSampTask(String taskName,@RequestParam(required = false) String targetUrl,@RequestParam(required = false) Integer period){
        SampAccessTask task = opcCollectService.addSampTask(taskName,period,targetUrl);
        return ResultEntity.successWithData(task);
    }

    /**
     *  获取当前Server 下所有任务
     */
    @GetMapping("/allSampTasks")
    public ResultEntity<List<SampAccessTask>> getAllSampTasks(@RequestParam(required = false) String progId){
        List<SampAccessTask> tasks  = opcCollectService.getAllSampTasks(progId);
        return ResultEntity.successWithData(tasks);
    }

    /**
     *  获取目标 task 信息
     * @param taskId 根据taskId
     */
    @GetMapping("/getItems/by/{taskId}")
    public ResultEntity<SampAccessTask> getTaskById(@PathVariable("taskId") Integer taskId){
        SampAccessTask task = opcCollectService.getTaskById(taskId);
        return ResultEntity.successWithData(task);
    }


    /**
     *  启动任务
     * @param taskId 任务Id
     */
    @GetMapping("/start/task/{id}")
    public ResultEntity<SampAccessTask> startSampTask(@PathVariable("id") Integer taskId){
        SampAccessTask task = opcCollectService.startSampTask(taskId);
        return ResultEntity.successWithData(task);
    }

    /**
     *  停止任务
     */
    @GetMapping("/stop/task/{id}")
    public ResultEntity<SampAccessTask> stopSampTask(@PathVariable("id") Integer taskId){
        SampAccessTask task = opcCollectService.stopSampTask(taskId);
        return ResultEntity.successWithData(task);
    }


    /**
     *  删除采集任务
     */
    @DeleteMapping("/delete/task/{id}")
    public ResultEntity<String> deleteSampTask(@PathVariable("id") Integer taskId){
        opcCollectService.deleteSampTaskById(taskId);
        return ResultEntity.successWithoutData();
    }


    //============================采集点相关================================================
    /**
     *  获取 目标OPCServer 下的所有 item
     */
    @GetMapping("/allItems")
    public ResultEntity<List<Item>> getAllItems(){
        List<Item> allItems = opcCollectService.getAllItems();
        return ResultEntity.successWithData(allItems);
    }

    /**
     *  添加采集点 到任务中
     * @param itemTask  目标采集点 和 目标任务
     */
    @PostMapping("/addItemToTask")
    public ResultEntity<SampAccessTask> addItemToTask(@RequestBody ItemTask itemTask){
        SampAccessTask task = opcCollectService.addItemToTask(itemTask);
        return ResultEntity.successWithData(task);
    }

    /**
     * 删除采集任务中的 采集点
     */
    @DeleteMapping("/deleteItem")
    public ResultEntity<String> deleteSampItemFromTask(@RequestBody ItemTask itemTask){
        opcCollectService.deleteSampItemFromTask(itemTask);
        return ResultEntity.successWithoutData();
    }


}
